package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.model.InterfaceLog;

import core.CoreDao;
import core.db.DBConnectionUtil;

public class InterfaceLogDao {

	public static void main(String[] args) {

	}
	

	public int insert(CoreDao dao, InterfaceLog bean) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_log_interface(name, time, during, result_status, result_desc, request_msg, create_time, ip,caller)");
			sql.append("VALUES(?, ?, ?, ?, ?, ?, sysdate(),?,?)");

			String resultDesc = bean.getResult_desc();
			if(StringUtils.isNotBlank(resultDesc)){          //json对象在转化为bean的时候会出错被置空
				resultDesc = resultDesc.replace("{", "[");
				resultDesc = resultDesc.replace("}", "]");
			}
			
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { 
						bean.getName(), bean.getTime(), bean.getDuring(), bean.getResult_status(), 
						resultDesc, bean.getRequest_msg(), bean.getIp(),bean.getCaller()
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	public int getTotalAcount(CoreDao dao, String paramSql) {
        if(StringUtils.isNotBlank(paramSql)){
            paramSql = " and " + paramSql;
        }
        Connection con = null;
        int result = -1;
        try {
            con = DBConnectionUtil.getDBConnection("emate1");
            StringBuffer sql = new StringBuffer(
                    " select count(1) as totalAcount "
                    + " from t_log_interface where 1=1 "
                    + paramSql );

            List<JSONObject> list = dao.serviceJsonSql(
                    con,
                    sql.toString(),
                    new Object[] { });

            JSONObject obj = list.get(0);
            result = (Integer) obj.get("totalAcount");

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DBConnectionUtil.close(null, null, con);
        }
        return result;
    }
	
	public List<InterfaceLog> queryAll(CoreDao dao, String paramSql, String pageSql) {

		Connection con = null;
		List<InterfaceLog> result = null;
		if(StringUtils.isNotBlank(paramSql)){
			paramSql = " and " + paramSql;
		}
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select * from t_log_interface where 1=1 "
					+ paramSql
					+ " order by create_time desc "
					+ pageSql );
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { });

			if (list != null && list.size() > 0) {
				result = dao.covertToBean(list, InterfaceLog.class);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

}
